//
//  HostRecommendView.m
//  NewPro
//
//  Created by RYX on 2019/5/27.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "HostRecommendView.h"
#import "TYCyclePagerView.h"
#import "HostRecommendCell.h"
#import "RecommendModel.h"
#import "ActivityDetailController.h"
#import "HomeList.h"
#import "CreditCardListViewController.h"
#import "HomeList.h"
#import "CreditCardListViewController.h"
#import "EtcViewController.h"

@interface HostRecommendView ()<TYCyclePagerViewDelegate,TYCyclePagerViewDataSource>

@property (nonatomic,strong) TYCyclePagerView *pagerView;
@property (nonatomic, copy) NSArray *datas;

@end

@implementation HostRecommendView

- (TYCyclePagerView *)pagerView{
    
    if (!_pagerView) {
        
        _pagerView = [[TYCyclePagerView alloc] init];
        _pagerView.isInfiniteLoop = NO;
        _pagerView.autoScrollInterval = 0.0;
        _pagerView.dataSource = self;
        _pagerView.delegate = self;
        [_pagerView registerClass:[HostRecommendCell class] forCellWithReuseIdentifier:@"cellId"];
        [self addSubview:_pagerView];
    }
    return _pagerView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColor.orangeColor;
    [self addSubview:line];
    
    UILabel *la2 = [[UILabel alloc] init];
    la2.font = [UIFont systemFontOfSize:14];
    la2.attributedText = [StrTool getAttributedTextWithTitle:@"热门推荐" color:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0] fontSize:14];
    [self addSubview:la2];
    
//    UIButton *btn = [[UIButton alloc] init];
//    [btn setTitle:@"更多" forState:UIControlStateNormal];
//    btn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size: 12];
//    [btn setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(onMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:btn];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self).offset(14);
        make.width.mas_equalTo(1.5);
        make.height.mas_equalTo(14);
    }];
    
    [la2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(line.mas_right).offset(2);
        make.centerY.equalTo(line);
    }];
    
//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.right.equalTo(self).offset(-14);
//        make.centerY.equalTo(line);
//    }];
    
    [self.pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self);
        make.top.equalTo(la2.mas_bottom).offset(10);
        make.bottom.equalTo(self).offset(-10);
    }];
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    
    return _datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    
    RecommendModel *model = _datas[index];
    
    HostRecommendCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    [cell.im sd_setImageWithURL:[NSURL URLWithString:model.img]];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(ScreenWidth - 28, CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 9;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    //    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
    
    if (![[HomeList sharedInstance].hadRealName isEqualToString:@"4"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(judgeIfHadRealName)]) {
            
            [self.delegate judgeIfHadRealName];
            return;
        }
        return;
    }
    
    RecommendModel *model = _datas[index];
    
    [HomeList sharedInstance].recommendModel = model;
    
    if ([model.isParent isEqualToString:@"1"]) {
        
        CreditCardListViewController *VC = [[CreditCardListViewController alloc]init];
        [VC setHidesBottomBarWhenPushed:YES];
        [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
    }else{
        
        EtcViewController *VC = [[EtcViewController alloc]init];
        [VC setNavBarWithTitle:model.name];
        [VC setHidesBottomBarWhenPushed:YES];
        [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
    }
    
}

- (void)setListArr:(NSArray *)listArr{
    
    _datas = listArr;
    [self.pagerView reloadData];
}


#pragma mark - 更多
- (void)onMoreBtn:(UIButton *)sender{
    
    if (![[HomeList sharedInstance].hadRealName isEqualToString:@"4"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(judgeIfHadRealName)]) {
            
            [self.delegate judgeIfHadRealName];
            return;
        }
        return;
    }
    
    CreditCardListViewController *VC = [[CreditCardListViewController alloc] init];
    [VC setHidesBottomBarWhenPushed:YES];
    [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
}

@end
